#!/bin/zsh

# run test cases by junit5 console launcher

if ! java -version >/dev/null 2>&1; then
  echo "java env error"
  exit 1
fi

TARGET_DIR="target/"
CUR_FILE_PATH=$(dirname "$0")
if [ "${CUR_FILE_PATH}" = "." ]; then
  TARGET_DIR="../target/"
fi

# check repository dir
REPOSITORY="${HOME}/.m2/repository"
if [ ! -d "${REPOSITORY}" ]; then
  echo "${REPOSITORY} is not exist"
  exit 2
fi

# mvn test-compile
FAILURE_TIP="BUILD FAILURE"
result=$( (mvn compile && mvn test-compile) | grep "${FAILURE_TIP}")
if [[ ${result} =~ ${FAILURE_TIP} ]]; then
  echo "mvn build failure"
  exit 3
fi

DEPEND_PKG="${REPOSITORY}/ch/qos/logback/logback-classic/1.3.5/logback-classic-1.3.5.jar:\
${REPOSITORY}/ch/qos/logback/logback-core/1.3.5/logback-core-1.3.5.jar:\
${REPOSITORY}/org/slf4j/slf4j-api/2.0.4/slf4j-api-2.0.4.jar"
JUNIT_CONSOLE_LAUNCHER="${CUR_FILE_PATH}/junit-platform-console-standalone-1.8.1.jar"

echo "==============开始执行测试==============="
# run test cases
java -jar "${JUNIT_CONSOLE_LAUNCHER}"\
  -cp "${DEPEND_PKG}"\
  -cp "${TARGET_DIR}test-classes:${TARGET_DIR}classes"\
  -c zzw.part2.cases.TestCalculator

echo "==============测试执行完毕==============="
